import jwt from 'jsonwebtoken';

const secret = 'your_jwt_secret'; // Replace with your secret key

export const generateToken = (payload: object, expiresIn: string | number = '7day'): string => {
  return jwt.sign(payload, secret, { expiresIn });
};

export const verifyToken = (token: string): object | string => {
  return jwt.verify(token, secret);
};

export const decodeToken = (token: string): any => {
  return jwt.decode(token);
};

export const getIdByToken = (token: string): number => {
  try {
    const decoded = decodeToken(token.replace('Bearer ', ''));
    return decoded.userId || null;
  } catch (error) {
    return null;
  }
};
